unit LangObj;

(* ======================================================== *)
(* TLangObject                                              *)
(* ======================================================== *)
(*   use this module with TLangMgr, support multi language  *)
(* base object for your application.                        *)
(*   tony                                                   *)
(*   2003.03.17                                             *)
(* ======================================================== *)

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, IniFiles, LangMgr;

type
  TLangObject = class(TObject, ILangIntf)
  private
    AIniFile:TIniFile;
    function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  protected
    procedure DoChangeLanguage(); virtual;
    function Translate(Ident:String;Default:String):String; overload;
    function Translate(Ident:String;Default:String;const Args:array of const):String; overload;
  public
    constructor Create();virtual;
    destructor Destroy(); override;
    procedure GetLanguageList(LangList:TStringList;var LangIndex:Integer);
    procedure ChangeLanguage(LangIndex:Integer);
  end;

implementation

{ TLangObj }

//no use
//2003.03.17
function TLangObject.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  Result := S_OK;
end;

//no use
//2003.03.17
function TLangObject._AddRef: Integer;
begin
  Result := -1;
end;

//no use
//2003.03.17
function TLangObject._Release: Integer;
begin
  Result := -1;
end;

//change ini file
//2003.03.17
procedure TLangObject.DoChangeLanguage();
var
  FileName:String;
begin
  if Assigned(AIniFile) then begin
    FreeAndNil(AIniFile);
  end;
  FileName:=ALangMgr.GetActiveLangFileName();
  if FileExists(FileName) then begin
    AIniFile:=TIniFile.Create(FileName);
  end;
end;

//translate string
//2003.03.17
function TLangObject.Translate(Ident:String;Default:String):String;
begin
  Result:=Default;
  try
    if Assigned(AIniFile) then begin
      Result:=AIniFile.ReadString(ClassName,Ident,Default);
    end;
  except
  end;
end;

//translate string with format
//2003.03.17
function TLangObject.Translate(Ident:String;Default:String;const Args:array of const):String;
var
  StrTmp:String;
begin
  try
    StrTmp:=Translate(Ident,Default);
    Result:=Format(StrTmp,Args);
  except
    Result:=StrTmp;
  end;
end;

//constructor
//2003.03.17
constructor TLangObject.Create();
begin
  inherited Create;
  ALangMgr.AddLangObj(self);
  DoChangeLanguage();
end;

//destructor
//2003.03.17
destructor TLangObject.Destroy();
begin
  AlangMgr.RemoveLangObj(self);
  AIniFile.Free;
  inherited Destroy;
end;

//call the language manager to get language list and active language's index
//2003.03.18
procedure TLangObject.GetLanguageList(LangList:TStringList;var LangIndex:Integer);
begin
  ALangMgr.GetLanguageList(LangList,LangIndex);
end;

//call the laguage manager to change the language
//2003.03.18
procedure TLangObject.ChangeLanguage(LangIndex:Integer);
begin
  ALangMgr.ChangeLanguage(LangIndex);
end;

end.
